<template>
  <div class="container" v-loading="loading">
    <h2>{{ title }}</h2>
    <StepWatermark ref="stepRef"></StepWatermark>
    <div class="content">
      <!--上传文件-->
      <SelectFile v-if="stepRef?.active === 0" :accept="fileAccept" @after="start"></SelectFile>
      <!--添加水印-->
      <el-result v-if="stepRef?.active === 1" icon="success" title="文件上传成功">
        <template #extra>
          <el-button type="primary" @click="addWatermark"
            >添加水印
            <el-icon class="el-icon--right">
              <ArrowRight />
            </el-icon>
          </el-button>
        </template>
      </el-result>
      <!--保存文件-->
      <el-result v-if="stepRef?.active === 2" icon="success" title="水印添加成功">
        <template #extra>
          <el-button type="primary" @click="save"
            >去保存
            <el-icon class="el-icon--right">
              <ArrowRight />
            </el-icon>
          </el-button>
        </template>
      </el-result>
    </div>
  </div>
</template>

<script lang="ts" setup>
import StepWatermark from '@/components/StepWatermark.vue'
import SelectFile from '@/components/SelectFile.vue'
import type { UploadFile } from 'element-plus'
import { useHandleWatermark } from '@/hooks/useHandleWatermark'
import { ref, unref } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'

defineProps({
  title: {
    type: String,
  },
  fileAccept: {
    type: String,
  },
})

const stepRef = ref()
const { fileRes, watermarkFile, saveFile } = useHandleWatermark()

const start = (file: UploadFile) => {
  fileRes.value = file
  unref(stepRef).updateActive(1)
}

const loading = ref<boolean>(false)
const addWatermark = () => {
  loading.value = true
  watermarkFile().then(() => {
    loading.value = false
    unref(stepRef).updateActive(2)
  })
}

const save = () => {
  saveFile().then(() => {
    unref(stepRef).updateActive(0)
  })
}
</script>

<style scoped lang="less">
.container {
  h2 {
    text-align: left;
    margin-bottom: 20px;
  }
  .content {
    margin-top: 20px;
  }
}
</style>
